/**
 * Created by jinxs on 2015.01.27.
 */
(function ($) {
    /**
     * 缓存名称
     * @type {string}
     * @private
     */
    var _cacheName = 'uiCheckbox';
    /**
     * @name uiCheckbox
     * @class 复选框
     * @constructor
     * @summary 构造函数
     * @param {Object|String} options 如果为Object，则表示组件配置，如果为String，则调用组件方法
     * @param {Object} param 当options为String时，可以传入方法参数
     * @returns {jQuery} jQuery对象
     * @example
     *  //采用默认值
     * $('#cb').uiCheckbox();
     * //设置组件参数
     *  $('#cb').uiCheckbox({id:'cb',label:'提交'});
     * //调用组件方法
     * $('#cb').uiCheckbox('setLabel','保存');
     */
    $.fn.uiCheckbox = function (options, param) {
        if (typeof options == 'string') {
            return $.fn.uiCheckbox.methods[options](this, param);
        }
        return this.each(function(){
            var me = $(this);
            var state = me.data(_cacheName);

            options = options || {};
            if (state) {
                $.extend(state.options, options);
            } else {
                me.data(_cacheName, {
                    options: $.extend(true, {}, $.fn.uiCheckbox.defaults, me.parseUIConfig(), options)
                });
            }
            _init(me)
        });
    };
    /**
     * 方法
     * @lends uiCheckbox.prototype
     */
    $.fn.uiCheckbox.methods = {
        /**
         * @summary 获取组件配置参数
         * @function
         * @name uiCheckbox#options
         * @returns {Object}
         * @example
         *  $('#cb').uiCheckbox('options');
         */
        options: function (target) {
            return $.data(target[0], _cacheName).options;
        },
        /**
         * @summary 启用组件
         * @function
         * @name uiCheckbox#enable
         * @returns {jQuery} 方法调用者
         */
        enable: function (target) {
            _setDisabled(target, false);
            return target;
        },
        /**
         * @summary 禁用组件
         * @function
         * @name uiCheckbox#disable
         * @returns {jQuery} 方法调用者
         */
        disable: function (target) {
            _setDisabled(target, true);
            return target;
        },
        /**
         * @summary 设置是否禁用组件
         * @function
         * @name uiCheckbox#setDisabled
         * @param disabled {Boolean} 是否禁用
         * @returns {jQuery} 方法调用者
         */
        setDisabled: function (target, disabled) {
            _setDisabled(target, !!disabled);
            return target;
        }
    };
    /**
     * 默认配置
     * @lends uiCheckbox.prototype
     */
    $.fn.uiCheckbox.defaults =
    {
        disabled: false,
        readonly: false,
        beforeClick: function () {
        },
        onChanged:function(checked){
        }
    };

    //////--------------------------------内部方法------------------------------
    /**
     * 创建按钮
     * @param target 目标对象
     */
    var _init = function (target) {
        var options = $.data(target[0], _cacheName).options;
        target.setElemID();
        var targetID = target.getElemID();

        target.addClass('ui_checkboxOrigin');
        var reg = new RegExp('({uiCheckbox})', 'g');
        target.wrap($($.uiTemplate.uiCheckbox.wrapperHtml.replace(reg, targetID)));
        target.before($($.uiTemplate.uiCheckbox.cbHtml.replace(reg, targetID)));

        var wrapper = $.getByParentID(targetID, $.uiTemplate.uiCheckbox.wrapperID);
        var cb = $.getByParentID(targetID, $.uiTemplate.uiCheckbox.cbID);
        cb.click(function () {
            if (target.attr('disabled') || target.attr('readonly')) {
                return false;
            }
            if (options.disabled || options.readonly) return false;
            if (options.beforeClick.call(target) == false) return false;
            if ($(this).hasClass("ui_checkbox-checked")) {
                _setValue(target, false);
            }
            else {
                _setValue(target, true);
            }
            target.trigger("change");
            options.onChanged.call(target,_getValue(target));
        });
        wrapper.hover(function () {
            if (!options.disabled)
                $(this).addClass("checkbox_over");
        }, function () {
            $(this).removeClass("checkbox_over");
        });
        _updateStyle(target);
    }

    var _updateStyle = function (target) {
        var options = $.data(target[0], _cacheName).options;
        var targetID = target.getElemID();
        if (target.attr('disabled')) {
            var wrapper = $.getByParentID(targetID, $.uiTemplate.uiCheckbox.wrapperID);
            wrapper.addClass("checkbox-disabled");
            options.disabled = true;
        }
        var cb = $.getByParentID(targetID, $.uiTemplate.uiCheckbox.cbID);
        if (target[0].checked) {
            cb.addClass('ui_checkbox-checked');
        }
        else {
            cb.removeClass('ui_checkbox-checked');
        }
    };
    var _setValue = function (target, value) {
        var targetID = target.getElemID();
        var cb = $.getByParentID(targetID, $.uiTemplate.uiCheckbox.cbID);
        if (!value) {
            target[0].checked = false;
            cb.removeClass('ui_checkbox-checked');
        }
        else {
            target[0].checked = true;
            cb.addClass('ui_checkbox-checked');
        }
    };
    var _setDisabled = function (target, value) {
        var targetID = target.getElemID();
        var wrapper = $.getByParentID(targetID, $.uiTemplate.uiCheckbox.wrapperID);
        if (value) {
            target.attr('disabled', true);
            wrapper.addClass("checkbox-disabled");
        }
        else {
            target.attr('disabled', false);
            wrapper.removeClass("checkbox-disabled");
        }
    };
    var _getValue = function (target) {
        return target[0].checked;
    };
})(jQuery);